package com.yq.ylmall.product.utils;

import cn.hutool.core.bean.BeanUtil;
import com.yq.ylmall.model.product.Region;
import com.yq.ylmall.vo.product.CategoryVo;
import com.yq.ylmall.vo.product.RegionVo;

import java.util.ArrayList;
import java.util.List;

/**
 * 地区多级菜单处理
 */
public class RegionHelper {
    /**
     * 使用递归方法建菜单
     * @param regionList List<Region>
     * @return List<Region>
     */

    public static List<RegionVo> buildTree(List<Region> regionList){
        List<RegionVo> trees = new ArrayList<>();
        for (Region region : regionList) {
            if (region.getParentCode() !=null && region.getParentCode() == 0){
                trees.add(findChildren(region,regionList));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     * @param region {@link Region}
     * @param treeNodes List<Region>
     * @return {@link Region}
     */
    public static RegionVo findChildren(Region region,List<Region> treeNodes){
        region.setChildren(new ArrayList<>());
        for (Region it : treeNodes) {
            if (region.getParentCode()!=null && (region.getCode().longValue() ==it.getParentCode().longValue())){
                region.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return BeanUtil.copyProperties(region,RegionVo.class);
    }




}
